directive.directive('validate',[function(){
    var errorClass="error flash animated";

    var defaultOptions={
        timely:false,
        stopOnError: true,
        ignore: ':hidden',
        msgMaker: function(opt) {
            core.ui.alert.warning(opt.msg);
            return '';
        },
        msgShow: function($msgbox, type) {
            var input=$msgbox.parent();
            input.addClass(errorClass);
            $msgbox.hide();
            return false;
        },
        msgHide:function($msgbox, type) {
            var input=$msgbox.parent();
            input.removeClass(errorClass);
            return false;
        }
    };

    return {
        restrict:'A',
        link:function($scope,$element,$attrs){
            var validCallback=$element.attr('valid');
            if(stringUtil.isBlank(validCallback)) {
                return;
            }

            var items=$element.find('[name][name!=""][rule][rule!=""]');
            if(objectUtil.isEmpty(items)) {
                return;
            }

            var fields={};
            for(var i=0;i<items.length;i++) {
                var item=$(items[i]);
                var name=item.attr('name');
                var rule=item.attr('rule');

                fields[name]={
                    rule:rule,
                    msg:(function(){
                        var messages={};

                        var message=item.attr('valid-msg');
                        if(stringUtil.isBlank(message)) {
                            return messages;
                        }

                        var rules=rule.split(';');
                        for(ruleIndex in rules) {
                            var ruleName=rules[ruleIndex];
                            if(stringUtil.isBlank(ruleName)) {
                                continue;
                            }

                            if(ruleName.indexOf('[')!=-1) {
                                messages[ruleName.substring(0,ruleName.indexOf('['))]=message;
                            }
                            else {
                                messages[ruleName]=message;
                            }
                        }

                        return messages;
                    })()
                };
            }

            var options={
                invalidClass:'error',
                fields:fields,
                ignore:"",
                valid:function(form) {
                    eval('$scope.'+validCallback.replace('()',''))($scope,form);
                },
                invalid:function(form,errors){
                    var invalidCallback=$element.attr('invalid');
                    if(stringUtil.isBlank(invalidCallback)) {
                        return;
                    }

                    eval('$scope.'+invalidCallback.replace('()',''))($scope,form,errors);
                }
            };

            options=$.extend({},defaultOptions,options);
            $element.validator(options);
        }
    }
}]);